package model

import (
	"database/sql"
	"errors"
	"fmt"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"strconv"
)

type TblFakeRouteInfo struct {
	Id         int     `json:"id" db:"id" cbor:"id"`
	Game       string  `json:"game" db:"game" cbor:"game"`
	Name       string  `json:"name" db:"name" cbor:"name"`
	PlatformId int     `json:"platform_id" db:"platform_id" cbor:"platform_id"`
	Fee        float64 `json:"fee" db:"fee" cbor:"fee"`
	Priority   string  `json:"priority" db:"priority" cbor:"priority"`
	Status     int     `json:"status" db:"status" cbor:"status"`
	Test       int     `json:"test" db:"test" cbor:"test"`
}

type TblFakeRouteInfoParam struct {
	Page     int `json:"page"`
	PageSize int `json:"page_size"`

	Id         string  `json:"id" db:"id" cbor:"id"`
	Game       string  `json:"game" db:"game" cbor:"game"`
	Name       string  `json:"name" db:"name" cbor:"name"`
	PlatformId int     `json:"platform_id" db:"platform_id" cbor:"platform_id"`
	Fee        float64 `json:"fee" db:"fee" cbor:"fee"`
	Priority   string  `json:"priority" db:"priority" cbor:"priority"`
	Status     int     `json:"status" db:"status" cbor:"status"`
	Test       int     `json:"test" db:"test" cbor:"test"`
}

type TblFakeRouteInfoList struct {
	D []TblFakeRouteInfo `json:"d" cbor:"d"`
	T int                `json:"total" cbor:"t"`
}

func GetTblFakeRouteInfo(param TblFakeRouteInfoParam, ctx *fasthttp.RequestCtx) (TblFakeRouteInfo, error) {
	data := TblFakeRouteInfo{}
	table := "tbl_cfg_fake_route_info"
	where := "1=1"
	join := ""
	if param.Game != "" {
		where += " and a.game='" + param.Game + "'"
	}
	if param.Name != "" {
		where += " and a.name='" + param.Name + "'"
	}

	if param.Priority != "" {
		where += " and a.priority='" + param.Priority + "'"
	}
	if param.Test != 0 {
		where += " and a.test='" + strconv.Itoa(param.Test) + "'"
	}
	if param.Status != 0 {
		where += " and a.status=" + strconv.Itoa(param.Status)
	}
	if param.PlatformId != 0 {
		where += " and a.platform_id=" + strconv.Itoa(param.PlatformId)
	}

	field := "a.*"
	query := "select " + field + " from " + table + "  as a " + join + " where " + where
	err := meta.MerchantDB.Get(&data, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}
	return data, nil
}

func GetTblFakeRouteInfoList(param TblFakeRouteInfoParam, ctx *fasthttp.RequestCtx) (TblFakeRouteInfoList, error) {
	data := TblFakeRouteInfoList{}
	table := "tbl_cfg_fake_route_info"
	where := "1=1"
	join := ""
	oderBy := "a.id"
	oderType := "desc"
	if param.Game != "" {
		where += " and a.game='" + param.Game + "'"
	}
	if param.Name != "" {
		where += " and a.name='" + param.Name + "'"
	}

	if param.Priority != "" {
		where += " and a.priority='" + param.Priority + "'"
	}
	if param.Test != 0 {
		where += " and a.test='" + strconv.Itoa(param.Test) + "'"
	}
	if param.Status != 0 {
		where += " and a.status=" + strconv.Itoa(param.Status)
	}
	if param.PlatformId != 0 {
		where += " and a.platform_id=" + strconv.Itoa(param.PlatformId)
	}
	order := oderBy + " " + oderType

	offset := (param.Page - 1) * param.PageSize
	if param.Page >= 1 {
		count := "select count(1) from " + table + " as a where " + where
		err := meta.MerchantDB.Get(&data.T, count)
		if err != nil {
			return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), count), "数据库错误")
		}
		if data.T == 0 {
			return data, nil
		}
	}

	field := "a.*"
	query := "select " + field + " from " + table + "  as a " + join + " where " + where + " order by " + order + " limit " + strconv.Itoa(param.PageSize) + " offset " + strconv.Itoa(offset)
	err := meta.MerchantDB.Select(&data.D, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}
	return data, nil
}

func UpdateOrDelTblFakeRouteInfo(param TblFakeRouteInfo, isDelete int) error {
	table := "tbl_cfg_fake_route_info"
	query := ""
	if param.Id > 0 {
		if isDelete == 0 {
			query, _, _ = dialect.Update(table).Set(param).Where(g.Ex{"id": param.Id}).ToSQL()
		} else {

			query, _, _ = dialect.Delete(table).Where(g.Ex{"id": param.Id}).ToSQL()
		}
	} else if param.Id < 0 {
		query, _, _ = dialect.Update(table).Set(g.Ex{"status": param.Status}).Where(g.Ex{"game": param.Game}).ToSQL()
	} else {
		query, _, _ = dialect.Insert(table).Rows(param).ToSQL()
	}
	_, err := meta.MerchantDB.Exec(query)
	if err != nil {
		return errors.New("数据库错误")
	}
	return nil
}
